
#include "wx/wx.h" 
#include <wx/listctrl.h>
#include <wx/mediactrl.h>
#include <wx/dir.h>
#include "utils.h"
#include "PlayList.h"
#include "SongList.h"
#include "MainFrame.h"
#include <id3/tag.h>
#include "SongListFrame.h"
#include <irrKlang.h>
using namespace irrklang;
#pragma comment(lib, "irrKlang.lib") // link with irrKlang.dll

enum
{
    ID_Quit = 1,
    ID_About,
	ID_Play,
	ID_Prev,
	ID_Next,
	ID_Pause,
	ID_PLAYLIST,
	ID_TAG1,
	ID_MediaCtrl,
	ID_OUT,
	ID_AddMp3,
	ID_TIMER,
	ID_SONGLIST
};

BEGIN_EVENT_TABLE(MainFrame, wxFrame)
    EVT_MENU(ID_Quit, MainFrame::OnQuit)
    EVT_MENU(ID_About, MainFrame::OnAbout)
	EVT_MENU(ID_AddMp3, MainFrame::OnAddMp3)
	EVT_MENU(ID_Play, MainFrame::OnPlay)
	EVT_TIMER(ID_TIMER, MainFrame::OnTimer)
END_EVENT_TABLE()


MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size ) 
{
	/* Creates Songlist of all music imported */
	SongListFrame *songListFrame = new SongListFrame( _("Song List"), 
										 wxPoint(50, 50),
										 wxSize(400,800)
										);
	songListFrame->Show(true);
	wxBoxSizer *sizer1 = new wxBoxSizer(wxHORIZONTAL);
	wxBoxSizer *sizer2 = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer *sizer3 = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer *sizer4 = new wxBoxSizer(wxVERTICAL);

	/* Creates log file for MainFrame class. */ 
	FILE * pFileMainFrame;
	pFileMainFrame = fopen("Logs\\MainFrameLog.txt", "w");/* This should be checked for null. */
	fprintf(pFileMainFrame, "Debug MainFrame Log\n");
	fclose(pFileMainFrame);/* #FIXUP Figure out where this should go. */

	SetUpTimer(this);
	SetUpOutput(sizer4);
	SetUpToolbar();
	SetUpGUI(sizer1, sizer2, sizer3, sizer4);
	SetUpControls(sizer1);
	SetUpSongList(sizer2);
	SetUpTagList(sizer3);

	engine = irrklang::createIrrKlangDevice();

	 


	//SetTopWindow(songListFrame);
}

/************************** Menu item Functions *****************************/
void MainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	if ( wxMessageBox(_T("Are you sure you want to quit?\n"),
                          _T("Exit?"),
                          wxYES_NO, this) == wxYES )
	{
    Close(TRUE);
	}
 
}

void MainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox( _("This is a music player that uses tags to arrange a modular playlist."),
                  _("About Virtual Tabletop"),
                  wxOK, this);
}

void MainFrame::OnAddMp3(wxCommandEvent& WXUNUSED(event))
{
	wxDirDialog  *OpenDialog = new wxDirDialog (
								this, _ ("Choose a directory to import."), wxEmptyString,
								wxDD_DEFAULT_STYLE);
 
	if (OpenDialog->ShowModal() == wxID_OK) // if the user click "Open" instead of "Cancel"
	{
		
		CurrentDocPath = new wxString(OpenDialog->GetPath());
		songListFrame->ImportMusic(CurrentDocPath);
		//mySongList->addSong(CurrentDocPath);
		
		
	}
}

void MainFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
{
	mciSendString("play song1", NULL, 0, 0);
    SetStatusText( _("Playing.") );
}

void MainFrame::OnPause(wxCommandEvent& WXUNUSED(event))
{
	mciSendString("pause song1", NULL, 0, 0);
    SetStatusText( _("Paused.") );
}




void MainFrame::OnTimer(wxTimerEvent& event)
{
	//output->AppendText("HI");
	

}


void MainFrame::SetUpTimer(MainFrame* thisFrame)
{
	/* Timer Setup */
	myTimer = new wxTimer(thisFrame,ID_TIMER);
	myTimer->Start(TIMER_INTERVAL);
}

void MainFrame::SetUpOutput(wxBoxSizer *sizer4)
{
	/* Output field */
	output = new wxTextCtrl(this,ID_OUT, "", wxDefaultPosition, wxSize(300,300), wxTE_MULTILINE);
	/* Error Output Text Box */   
	sizer4->Add(output, 0, 0, 0);
}


void MainFrame::SetUpToolbar(void)
{
	/* Toolbar Menu Construction */
	wxMenu *menuFile = new wxMenu;
	wxMenuBar *menuBar = new wxMenuBar;
	menuBar->Append( menuFile, _("File") );
	menuFile->Append( ID_AddMp3, _("Add MP3") );
	menuFile->Append( ID_About, _("About") );
	menuFile->AppendSeparator();
    menuFile->Append( ID_Quit, _("Exit") );
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText( _("Tag Music Player") );



}

void MainFrame::SetUpGUI(wxBoxSizer *sizer1,wxBoxSizer *sizer2,
						 wxBoxSizer *sizer3,wxBoxSizer *sizer4)
{
	/* Layout Construction */
	wxGridSizer* mainFrameGrid = new wxGridSizer(2, 2, 0, 0);
    SetSizer(mainFrameGrid);
	
	mainFrameGrid->Add(sizer1,1,0,0);
	mainFrameGrid->Add(sizer2,1,0,0);
	mainFrameGrid->Add(sizer3,1,0,0);
	mainFrameGrid->Add(sizer4,1,0,0);



}

void MainFrame::SetUpControls(wxBoxSizer *sizer1)
{

	/* Media Controls */
	wxSize *buttonSize = new wxSize(40,20);
	wxButton *playButton = new wxButton( this, ID_Play, _("Play"), wxDefaultPosition, *buttonSize, 0 );
	wxButton *prevButton = new wxButton( this, ID_Prev, _("Prev"), wxDefaultPosition, *buttonSize, 0 );
    wxButton *nextButton = new wxButton( this, ID_Next, _("Next"), wxDefaultPosition, *buttonSize, 0 );
    wxButton *pauseButton = new wxButton( this, ID_Pause, _("Pause"), wxDefaultPosition, *buttonSize, 0 );
	
	sizer1->Add(playButton, 0, 0, 0);
	sizer1->Add(prevButton, 0, 0, 0);
	sizer1->Add(nextButton, 0, 0, 0);
	sizer1->Add(pauseButton, 0, 0, 0);
	
	Connect(ID_Play, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::OnPlay));
	Connect(ID_Pause, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainFrame::OnPause));


}
void MainFrame::SetUpSongList(wxBoxSizer *sizer2)
{
	/* SongList and Playlist Setup */
	mySongList = new SongList();

	/* Song List */
	wxListCtrl *myList = new wxListCtrl(this, ID_PLAYLIST, wxDefaultPosition,
										wxSize (500,250),wxLC_REPORT);

	wxListItem  col0;
    col0.SetId(0);
    col0.SetText( _ ("Song") );
    col0.SetWidth(50);
    myList->InsertColumn(0, col0);

	// Add second column
    wxListItem  col1;
    col1.SetId(1);
    col1.SetText( _ ("Artist") );
    myList->InsertColumn(1, col1);
    
    // Add thirs column     
    wxListItem  col2;
    col2.SetId(2);
    col2.SetText( _ ("Duration") );
    myList->InsertColumn(2, col2);
	
	int n =0;

	wxListItem item;
    item.SetId(n);
    item.SetText("BLAH BLAH BLAH");
	
    myList->InsertItem( item );
	myList->SetItem(n, 0, "Foo");
	myList->SetItem(n, 1, "Two");
	myList->SetItem(n, 2, "Three");
    sizer2->Add(myList,1, wxEXPAND | wxALL, 10);
}

void MainFrame::SetUpTagList(wxBoxSizer *sizer3)
{
	/* Tag List */
	wxCheckBox *tag1 = new wxCheckBox(this,ID_TAG1, _("Tag 1"), wxDefaultPosition,										wxDefaultSize, wxCHK_3STATE &&
									wxCHK_ALLOW_3RD_STATE_FOR_USER);
	sizer3->Add(tag1, 0, 0, 0);


}